package com.sbl.bajw.scheduler;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import com.sbl.bajw.common.MyBatisDao;
import com.sbl.bajw.common.PropertiesUnit;
import com.sbl.bajw.common.web.BaiduMsgPush;
import com.sbl.bajw.mapper.MsgBoxDao;
import com.sbl.bajw.mapper.StatisticsDao;
import com.sbl.bajw.mapper.TodoModelDao;
import com.sbl.bajw.mapper.UserDao;
import com.sbl.bajw.pc.model.MsgBoxModel;
import com.sbl.bajw.pc.model.UserModel;
import com.sbl.bajw.pc.service.JWTMsgPushService;
import com.sbl.bajw.util.LogUtil;

@Service
public class MsgSendJob {
	
	@Resource
	private TodoModelDao dao;
	
	@Resource
	private StatisticsDao statisticsDao;
	
	@Resource
	private UserDao udao;
	
	@Resource
	private MsgBoxDao mdao;
	
	@Resource
	JWTMsgPushService jwtmsgPushService;
	
	@Resource
	private MyBatisDao gridDao;
	
	static Logger log;
	
	static{
		log = LogUtil.getLogger("msgjob");
	}
	
	public void sendCameraAndHouseStatisticMsg(){
		
		log.info("摄像头监控室统计开始推送");
		
		String useridsStr = PropertiesUnit.getValue("sendStatistics_userid");
		String[] userids = useridsStr.split(",");
		
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
		String date = sf.format(new Date());
		HashMap<String, Object> parm = new HashMap<String, Object>();
		parm.put("startdate", date+" 00:00");
		parm.put("enddate", date+" 23:59");
		
		List<HashMap<String, Object>> result = null;
		
		try {
			result= statisticsDao.StatisticsCameraAndHouse(parm);
		} catch (Exception e) {
			log.info("出错了", e);
		}
		
		StringBuilder stringB = new StringBuilder();
		for(int i=0;i<result.size();i++){
			HashMap<String, Object> row = result.get(i);
			String deptname = row.get("DEPTNAME").toString();
			Object cameraCountO = row.get("CAMERA_COUNT");
			Object houseCountO = row.get("HOUSE_COUNT");
			String cameraCount = null;
			String houseCount = null;
			
			if(cameraCountO instanceof BigDecimal){
				cameraCount = ((BigDecimal)cameraCountO).intValue()+"";
			}
			else{
				cameraCount = "0";
			}
			
			if(houseCountO instanceof BigDecimal){
				houseCount = ((BigDecimal)houseCountO).intValue()+"";
			}
			else{
				houseCount = "0";
			}
			stringB.append(deptname+"：监控室:"+houseCount+"个,摄像头:"+cameraCount+"个\r\n");
		}
		String content = stringB.toString();
		log.info(stringB.toString()+","+result.size());
		for(int i =0;i<userids.length;i++){
			String userid = userids[i];
			UserModel userModel = udao.getModel(userid);
			
			MsgBoxModel mModel = new MsgBoxModel();
			mModel.setTitle("每日摄像头监控室统计");
			mModel.setContent(content);
			mModel.setCreater("685");
			mModel.setUserid(userid);
			
			mdao.insert(mModel);
			
			BaiduMsgPush.msgPush(userModel.getBaiduUserid(), userModel.getBaiduChannelid(), content, "今日采集统计","{\"todo\":\"camera_statistics\"}");
			log.info("摄像头监控室统计推送给"+userModel.getShowname());
		}
	}
	
	public void sendCameraAndHouseMsg() throws Exception{
		List<HashMap<String, String>> result = mdao.getCameraAndHouseUnpassMan();
		
		for(int i=0;i<result.size();i++){
			HashMap<String, String> row = result.get(i);
			String baiduuserid = row.get("BAIDU_USERID");
			String baiduchannelid = row.get("BAIDU_CHANNELID");
			String userid = row.get("ID");
			
			MsgBoxModel mModel = new MsgBoxModel();
			mModel.setTitle("采集任务提醒");
			mModel.setContent("您有未审核通过的采集任务，请及时处理。");
			mModel.setCreater("685");
			mModel.setUserid(userid);
			
			mdao.insert(mModel);
			
			BaiduMsgPush.msgPush(baiduuserid, baiduchannelid, mModel.getContent(), mModel.getTitle(),"{\"todo\":\"camera_modify\"}");
		}
	}
	
	public void sendStatistics(){
		String useridsStr = PropertiesUnit.getValue("sendStatistics_userid");
		
		String[] userids = useridsStr.split(",");

		HashMap<String, String> parm = new HashMap<String, String>();
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
		String dateStr = sf.format(new Date());
		parm.put("Start", dateStr+" 00:00");
		parm.put("End", dateStr+" 23:59");
		HashMap<String, Integer> allCount = dao.getAllCount(parm);
		HashMap<String, Integer> finishCount = dao.getFinishCount(parm);
		HashMap<String, Integer> outTimeCount = dao.getOutTimeCount(parm);
		
		int aCount = allCount.get("count")==null?0: allCount.get("count");
		int fCount = finishCount.get("count") == null?0:finishCount.get("count");
		int oCount = outTimeCount.get("count") == null?0:outTimeCount.get("count");
		Integer uCount = aCount - fCount;
		
		for(int i =0;i<userids.length;i++){
			String userid = userids[i];
			UserModel userModel = udao.getModel(userid);
			
			String content = "今日待办数:"+aCount+"、完成:"+fCount+"、未完成:"+uCount+"、超时未完成:"+oCount;
			
			MsgBoxModel mModel = new MsgBoxModel();
			mModel.setTitle("每日待办统计");
			mModel.setContent(content);
			mModel.setCreater("685");
			mModel.setUserid(userid);
			
			mdao.insert(mModel);
			
			BaiduMsgPush.msgPush(userModel.getBaiduUserid(), userModel.getBaiduChannelid(), content, "今日统计","{\"todo\":\"todo_statistics\"}");
		}
	}
}
